package com.cyam.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cyam.domain.Article;
import com.cyam.domain.ArticleFile;
import com.cyam.domain.ArticleType;
import com.cyam.service.ArticleFileService;
import com.cyam.service.ArticleService;
import com.cyam.service.ArticleTypeService;
import com.cyam.util.Pager;

/***
 * 这是系统管理/后台管理
 * @author CourageAQ
 *
 */
@Controller
@RequestMapping("/backblog")
public class SystemController {
	
	@Resource
	private ArticleService articleService;
	
	@Resource
	private ArticleTypeService articleTypeService;
	
	@Resource
	private ArticleFileService articleFileService;

	/**
	 * 文章列表---返回页面
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/index")
	public String viewIndex(Model model,HttpServletRequest request) throws Exception {
		HttpSession session = request.getSession();
		String view = "";
		if(session.getAttribute("username") != null) {
			Pager pages = articleService.countPager3(null,1, 12);
			model.addAttribute("pages", pages);
			view = "/system/index";
		}else {
			view = "/system/login";
		}
		return view;
	}
	/***
	 * 文章列表---分页json数据
	 * @param model
	 * @param page
	 * @param size
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	@ResponseBody
	public JSONObject listResult(Model model,
			@RequestParam(value = "page",defaultValue = "1") int page,
			@RequestParam(value ="size",defaultValue = "12") int size,Article article) throws Exception {
		JSONObject result = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("title", article.getTitle());
		map.put("type", article.getType());
		Pager pages = articleService.selectPage3(map, page, size);
		result.put("result", pages);
		return result;
	}
	
	/**查看文章
	 * @throws Exception **/
	@RequestMapping("/look")
	public String lookArticle(@RequestParam("id") int id,Model model) throws Exception {
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("a_id", id);
		ArticleFile articleFile = articleFileService.findOne(map);
		map.clear();
		map.put("id", id);
		Article article = articleService.findOne(map);
		model.addAttribute("articleFile", articleFile);
		model.addAttribute("article", article);
		return "/system/article-detail";
	}
	
	
	
	
	/**
	 * 文章分类--返回页面
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/type")
	public String viewType(Model model) throws Exception {
		Pager pages = articleTypeService.countPager(null, 1, 12);
		model.addAttribute("pages", pages);
		return "/system/type";
	}
	
	@RequestMapping("/listtype")
	@ResponseBody
	public JSONObject listType(Model model,
			@RequestParam(value = "page",defaultValue = "1") int page,
			@RequestParam(value ="size",defaultValue = "12") int size,
			@RequestParam(value = "type", required = false) String type) throws Exception {
		JSONObject result = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", type);
		Pager pages = articleTypeService.findPager(map, page, size);
		result.put("result", pages);
		return result;
	}
	
	/**
	 * 文章分类
	 * @return
	 */
	@RequestMapping("/upload")
	public String viewUpload() {
		return "/system/upload";
	}
	
	
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(@RequestParam("id") int id){
		ArticleType at = new ArticleType();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		Article article;
		try {
			article = articleService.findOne(map);
			if (StringUtils.isNoneBlank(article.getType())) {
				at.setId(Integer.valueOf(article.getType()));
				articleTypeService.update2(at);
			}
			articleService.remove(id);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping("/articletype")
	@ResponseBody
	public List<ArticleType> listType() throws Exception{
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("all", "all");
		List<ArticleType> list = articleTypeService.findAll(map);
		return list;
	}
	
}
